package com.ryh.controller;

import com.ryh.entity.Book;
import com.ryh.entity.Reader;
import com.ryh.service.BookService;
import com.ryh.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private BookService bookService;

    @Autowired
    private ReaderService ReaderService;

    @GetMapping("/books")
    public String showBooks(Model model) {
        List<Book> books = bookService.getAllBooks();
        model.addAttribute("books", books);
        return "admin/books";
    }

    @GetMapping("/book/{bookId}")
    public String showBookDetails(@PathVariable("bookId") String bookId, Model model) {
        Book book = bookService.getBookById(bookId);
        model.addAttribute("book", book);
        return "admin/bookDetails";
    }

    @GetMapping("/addBook")
    public String showAddBookForm(Model model) {
        model.addAttribute("book", new Book());
        return "admin/addBook";
    }

    @PostMapping("/addBook")
    public String addBook(@ModelAttribute("book") Book book) {
        bookService.addBook(book);
        return "redirect:/admin/books";
    }

    // 其他图书管理相关的方法，如更新图书、删除图书等

    @GetMapping("/Readers")
    public String showReaders(Model model) {
        List<Reader> Readers = ReaderService.getAllReaders();
        model.addAttribute("Readers", Readers);
        return "admin/Readers";
    }

    // 其他用户管理相关的方法，如更新用户、删除用户等
}